package com.xxxy.jsq.action.teacher;  // 修改包路径

import com.xxxy.jsq.dao.TeacherDao;  // 修改导入路径
import com.xxxy.jsq.model.Teacher;  // 修改导入路径

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class addTeacherServlet
 */
@WebServlet("/addTeacherServlet")
public class addTeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public addTeacherServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置请求和响应的字符编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		// 创建 Teacher 模型对象
		Teacher model = new Teacher();
		// 创建 TeacherDao 对象
		TeacherDao dao = new TeacherDao();

		// 获取页面数据
		// 获取页面数据（与前端表单字段名保持一致）
		String teacherName = request.getParameter("cname");        // 对应前端 name="cname"
		String departmentId = request.getParameter("pdnames");     // 对应前端 name="pdnames"
		String teacherGender = request.getParameter("tgender");    // 对应前端 name="tgender"
		String teacherPhone = request.getParameter("tphone");      // 对应前端 name="tphone"
		String teacherNum = request.getParameter("tnum");          // 对应前端 name="tnum"
		String teacherFlag = request.getParameter("cflag");        // 对应前端 name="cflag"

		// 将获取到的数据设置到 Teacher 模型对象中
		if (teacherName != null && !teacherName.equals("")) {
			model.setTeacherName(teacherName);
		}
		if (departmentId != null && !departmentId.equals("")) {
			model.setDepartmentId(departmentId);
		}
		if (teacherGender != null && !teacherGender.equals("")) {
			model.setTeacherGender(teacherGender);
		}
		if (teacherPhone != null && !teacherPhone.equals("")) {
			model.setTeacherPhone(teacherPhone);
		}
		if (teacherNum != null && !teacherNum.equals("")) {
			model.setTeacherNum(teacherNum);
		}
		if (teacherFlag != null && !teacherFlag.equals("")) {
			model.setTeacherFlag(teacherFlag);
		}

		// 调用 TeacherDao 的 add 方法添加教师信息
		String str = dao.add(model);

		// 获取响应输出流
		PrintWriter out = response.getWriter();
		// 输出结果
		out.print(str);
		// 刷新输出流
		out.flush();
		// 关闭输出流
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 调用 doGet 方法处理 POST 请求
		doGet(request, response);
	}
}